/**
 *  @file SDLGameSection.cpp
 *
 *  @date 24-nov-2009
 *  @author Ives van der Flaas
 */

#include "SDLGameSection.h"
#include "SDLGameEngine.h"
#include "SDLColor.h"
#include "SDLRectangleRepresentation.h"
#include "SDLZoomDrawer.h"

namespace si_sdl
{

SDLGameSection::SDLGameSection(SDLApplication& parent,
		SDLWindow& win):
	SDLApplicationSection(parent, win)
{
	fGameEngine = new SDLGameEngine();


}

SDLGameSection::~SDLGameSection()
{
	delete fGameEngine;
}
void SDLGameSection::onSDLEvent(SDL_Event& event)
{
	SDLApplicationSection::onSDLEvent(event); // make sure SDL_QUIT's are caught...

	// When user presses ESC goto menu
	if(event.type == SDL_KEYDOWN)
		if(event.key.keysym.sym == SDLK_ESCAPE)
			fParent.changeSectionTo(si::MENU);
}
void SDLGameSection::newFrame(double timeSinceLastFrame)
{
	si::FrameResult result = fGameEngine->newFrame(timeSinceLastFrame);

	// if the game just ended, doesn't matter why
	if(result == si::LOSTGAME || result == si::WONGAME)
	{
		fParent.gameEnded(fGameEngine->getScore());
	}

	if(result == si::WONGAME)
	{
		fParent.changeSectionTo(si::HIGHSCORES);

	}
	else if(result == si::LOSTGAME)
	{
		fParent.changeSectionTo(si::LOST);
	}


}

void SDLGameSection::youreStarting()
{
	fGameEngine->reset();

}
}
